@use 'sass:map';
@use 'sass:list';
@use 'sass:meta';

@function to-css-var-name($name-units...) {
  // variable prefix is fixed to '--vxp'
  $name: '--vxp';

  @each $unit in $name-units {
    @if $unit != '' {
      $name: $name + '-' + $unit;
    }
  }

  @return $name;
}

@function get-css-var($name-units, $default: null) {
  @if not $default {
    @return var(#{to-css-var-name($name-units...)});
  }

  @return var(#{to-css-var-name($name-units...)}, $default);
}

@function get-or-default($map, $default, $keys...) {
  $value: map.get($map, $keys...);

  @if not $value {
    @return $default;
  }

  @return $value;
}

@mixin define-css-var($name-units, $value) {
  #{to-css-var-name($name-units...)}: #{$value};
}

@mixin define-preset-values($base-name, $style-map, $inspect: false) {
  @each $name in map.keys($style-map) {
    @include define-css-var(
      ($base-name, $name),
      if($inspect, #{meta.inspect(map.get($style-map, $name))}, map.get($style-map, $name))
    );
  }
}

@mixin define-preset-style($base-name, $style-map) {
  @each $name in map.keys($style-map) {
    $style-units: map.get($style-map, $name);

    @if list.length($style-units) != 0 {
      @include define-css-var(($base-name, $name), get-css-var($style-units));
    }
  }
}
